package com.tsing.mapper.impl;

import com.tsing.common.framwork.mapper.CommonDao;
import com.tsing.mapper.AppUserMapper;
import com.tsing.pojo.dto.TlUserDTO;
import com.tsing.pojo.entity.TlCollect;
import com.tsing.pojo.entity.TlPost;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @auth 卢孝宇
 * @date 2023/12/13 8:55
 */
public class AppUserMapperImpl extends CommonDao implements AppUserMapper {


    @Override
    public Integer interestCount(Integer userId) {
        Integer list = null;

        Object[] params = {userId};
        try {
            list = executeCount("select count(*) from tl_interestid where followId = ?", params);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list;
    }

    @Override
    public Integer fansCount(Integer userId) {
        Integer list = null;

        Object[] params = {userId};
        try {
            list = executeCount("select count(*) from tl_interestid where userId = ?", params);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list;
    }

    @Override
    public Long collectCount(Integer userId) {
        int list = 0;

        Object[] params = {userId};
        try {
            list = executeCount("select count(*) from tl_collect where userId = ?", params);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return (long) list;
    }

    @Override
    public List<TlCollect> postId(Integer userId, Integer page, Integer size) {
        int offset = (page - 1) * size;
        List<TlCollect> list = null;

        Object[] params = {userId, offset, size};
        try {
            list = executeQuery(TlCollect.class, "select * from tl_collect where userId = ? limit ?,?", params);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list;
    }

    @Override
    public List<TlPost> noteList(Integer userId, Integer page, Integer size) {
        int offset = (page - 1) * size;
        List<TlPost> list = null;

        Object[] params = {userId, offset, size};
        try {
            list = executeQuery(TlPost.class, "select * from tl_post where userId = ? limit ?,?", params);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list;
    }

    @Override
    public Integer countAll(Integer userId) {
        Integer count = null;

        Object[] params = {userId};
        try {
            count = executeCount("select count(*) from tl_post where userId = ? ", params);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return count;
    }

    @Override
    public int update(TlUserDTO tlUserDTO) {
        int result = 0;
        Object[] params = {};

        if (tlUserDTO.getUserId() != null) {
            params = new Object[]{tlUserDTO.getUsername(),
                    tlUserDTO.getUserImage(),
                    tlUserDTO.getBookiPhone(),
                    tlUserDTO.getSignature(),
                    tlUserDTO.getIntroduction(),
                    tlUserDTO.getSex(),
                    tlUserDTO.getStatus(),
                    tlUserDTO.getUserId()};
        }

        try {
            result = executeUpdate("update tl_user " +
                    "set username = ?," +
                    " userImage = ?," +
                    " bookiPhone = ?," +
                    " signature = ?," +
                    " introduction = ?," +
                    " sex = ?," +
                    " status = ? " +
                    "where userId = ?", params);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        return result;
    }

    @Override
    public Integer updateUser(TlUserDTO tlUserDTO) {
        int result = 0;
        Object[] params = {};

        if (tlUserDTO.getUserId() != null) {
            params = new Object[]{tlUserDTO.getUsername(),
                    tlUserDTO.getUserImage(),
                    tlUserDTO.getBookiPhone(),
                    tlUserDTO.getSignature(),
                    tlUserDTO.getIntroduction(),
                    tlUserDTO.getSex(),
                    tlUserDTO.getStatus(),
                    tlUserDTO.getUserId()};
        }

        try {
            result = executeUpdate("update tl_user " +
                    "set username = ?," +
                    " userImage = ?," +
                    " bookiPhone = ?," +
                    " signature = ?," +
                    " introduction = ?," +
                    " sex = ?," +
                    " status = ? " +
                    "where userId = ?", params);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        return result;
    }
}
